using System;
using System.IO;
using System.Web.UI.WebControls;
using HeroldIT.Dnn.JQuery.UI;
using DotNetNuke.Services.Exceptions;
using System.Globalization;

namespace HeroldIT.Dnn.Modules.News
{
    public partial class NewsCoreSettings : Views.NewsCoreSettings
    {
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            this.Page.ClientScript.RegisterExpandoAttribute(((TextBox)this.txtMaxTeaserImageWidth).ClientID, "previousWidth", this.txtMaxTeaserImageWidth.Text, true);
            this.Page.ClientScript.RegisterExpandoAttribute(((TextBox)this.txtMaxTeaserImageHeight).ClientID, "previousHeight", this.txtMaxTeaserImageHeight.Text, true);
            this.Page.ClientScript.RegisterExpandoAttribute(((TextBox)this.txtMaxDetailImageWidth).ClientID, "previousWidth", this.txtMaxDetailImageWidth.Text, true);
            this.Page.ClientScript.RegisterExpandoAttribute(((TextBox)this.txtMaxDetailImageHeight).ClientID, "previousHeight", this.txtMaxDetailImageHeight.Text, true);

            ((TextBox)this.txtMaxCurrentItems).Attributes["onkeypress"] = UIHelper.GetNumOnlyHandlerScript(this.Page);
            ((TextBox)this.txtMaxDetailImageHeight).Attributes["onkeypress"] = UIHelper.GetNumOnlyHandlerScript(this.Page);
            ((TextBox)this.txtMaxDetailImageWidth).Attributes["onkeypress"] = UIHelper.GetNumOnlyHandlerScript(this.Page);
            ((TextBox)this.txtMaxTeaserImageHeight).Attributes["onkeypress"] = UIHelper.GetNumOnlyHandlerScript(this.Page);
            ((TextBox)this.txtMaxTeaserImageWidth).Attributes["onkeypress"] = UIHelper.GetNumOnlyHandlerScript(this.Page);

            ((Slider) this.txtTeaserImageQuality).ClientEvents.Slide =
                "jQuery('#" +
                ((Slider) this.txtTeaserImageQuality).ClientID +
                this.ClientIDSeparator +
                "value').html(ui.value)";
            ((Slider) this.txtDetailImageQuality).ClientEvents.Slide =
                "jQuery('#" + ((Slider) this.txtDetailImageQuality).ClientID +
                this.ClientIDSeparator +
                "value').html(ui.value)";
            ((Slider)this.txtMaxImagesToUse).ClientEvents.Slide =
                "jQuery('#" + ((Slider)this.txtMaxImagesToUse).ClientID +
                this.ClientIDSeparator +
                "value').html(ui.value)";

            JQuery.JQuery.RegisterJQueryOnDocumentReadyBlock(this.Page, "jQuery('#" + ((TextBox)this.txtTeaserImageMatte).ClientID + "').change(function(){jQuery('#" + ((TextBox)this.txtTeaserImageMatte).ClientID + this.ClientIDSeparator + "preview').css({backgroundColor:this.value})})");
            JQuery.JQuery.RegisterJQueryOnDocumentReadyBlock(this.Page, "jQuery('#" + ((TextBox)this.txtDetailImageMatte).ClientID + "').change(function(){jQuery('#" + ((TextBox)this.txtDetailImageMatte).ClientID + this.ClientIDSeparator + "preview').css({backgroundColor:this.value})})");
        }

        public override void LoadSettings()
        {
            base.LoadSettings();

            this.RefreshTeaserImageFolderCreateButton();
            this.RefreshDetailImageFolderCreateButton();
        }

        protected void RefreshTeaserImageFolderCreateButton()
        {
            if (!System.IO.Directory.Exists(Server.MapPath(this.PortalSettings.HomeDirectory + this.MakeFolderUrl(this.txtTeaserImageFolder.Text))))
            {
                this.btnCreateTeaserImageFolder.Visible = true;
                this.btnCreateTeaserImageFolder.Text = this.GetString("btnCreateImageFolder.Text");
            }
            else
            {
                this.btnCreateTeaserImageFolder.Visible = false;
            }
            this.lblCreateTeaserImageFolderMessage.Visible = false;
        }

        protected void txtTeaserImageFolder_TextChanged(object sender, EventArgs e)
        {
            this.RefreshTeaserImageFolderCreateButton();
        }

        protected void btnCreateTeaserImageFolder_Click(object sender, EventArgs e)
        {
            try
            {
                this.CreateFolder(this.txtTeaserImageFolder.Text);
                this.lblCreateTeaserImageFolderMessage.Text = this.GetString("litFolderCreated.Text");
                this.lblCreateTeaserImageFolderMessage.Visible = true;
                this.btnCreateTeaserImageFolder.Visible = false;
            }
            catch (Exception ex)
            {
                Exceptions.LogException(ex);
                this.lblCreateTeaserImageFolderMessage.Text =
                    String.Format(
                        CultureInfo.CurrentUICulture,
                        this.GetString("litFolderCreateError.Format"),
                        ex.Message);
                this.lblCreateTeaserImageFolderMessage.Visible = true;
            }
        }

        protected void RefreshDetailImageFolderCreateButton()
        {
            if (!System.IO.Directory.Exists(Server.MapPath(this.PortalSettings.HomeDirectory + this.MakeFolderUrl(this.txtDetailImageFolder.Text))))
            {
                this.btnCreateDetailImageFolder.Visible = true;
                this.btnCreateDetailImageFolder.Text = this.GetString("btnCreateImageFolder.Text");
            }
            else
            {
                this.btnCreateDetailImageFolder.Visible = false;
            }
            this.lblCreateDetailImageFolderMessage.Visible = false;
        }

        protected void txtDetailImageFolder_TextChanged(object sender, EventArgs e)
        {
            this.RefreshDetailImageFolderCreateButton();
        }

        protected void btnCreateDetailImageFolder_Click(object sender, EventArgs e)
        {
            try
            {
                this.CreateFolder(this.txtDetailImageFolder.Text);
                this.lblCreateDetailImageFolderMessage.Text = this.GetString("litFolderCreated.Text");
                this.lblCreateDetailImageFolderMessage.Visible = true;
                this.btnCreateDetailImageFolder.Visible = false;
            }
            catch (Exception ex)
            {
                Exceptions.LogException(ex);
                this.lblCreateDetailImageFolderMessage.Text =
                    String.Format(
                        CultureInfo.CurrentUICulture,
                        this.GetString("litFolderCreateError.Format"),
                        ex.Message);
                this.lblCreateDetailImageFolderMessage.Visible = true;
            }
        }
    }
}